﻿Public Class LoginForm

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        If My.Settings.dbname = "" Then
            MessageBox.Show("Maaf, database belum didefinisikan. Silahkan definisikan terlebih dahulu")
            Return
        End If
        If UserNameTextBox.Text <> "" Then
            If PasswordTextBox.Text <> "" Then
                Dim dt As New DataTable
                If Utils.executeSP("getlogin", New Object() {UserNameTextBox.Text, PasswordTextBox.Text}, dt) Then
                    If dt.Rows.Count > 0 Then
                        'login berhasil, hapus history view
                        MainForm.history.Clear()
                        Session.username = dt.Rows(0)("username") 'or the usernameTextBox
                        'Session.vusername = dt.Rows(0)("namalengkap")
                        Session.password = dt.Rows(0)("pass")
                        Session.kodeRole = dt.Rows(0)("koderole")
                        Session.rolename = dt.Rows(0)("role")


                        'TOFIX : ini spy cepet aja
                        Dim dtN As New DataTable
                        If Utils.executeSP("proc_profil", New Object() {"load", Convert.DBNull, Convert.DBNull, Convert.DBNull}, dtN) Then
                            If dtN IsNot Nothing Then
                                If dtN.Rows.Count > 0 Then
                                    Session.vcompany = dtN.Rows(0)("nama")
                                    Session.valamat = dtN.Rows(0)("alamat")
                                    Session.vtelepon = dtN.Rows(0)("telepon")
                                    'Session.vfax = dtN.Rows(0)("fax")
                                Else
                                    Session.vcompany = ""
                                    Session.valamat = ""
                                    Session.vtelepon = ""
                                    Session.vfax = ""
                                End If
                            Else
                                Session.vcompany = ""
                                Session.valamat = ""
                                Session.vtelepon = ""
                                Session.vfax = ""
                            End If
                        End If



                        MainForm.setLoggedIn(True)

                        UserNameTextBox.Text = ""
                        PasswordTextBox.Text = ""

                        MainForm.setFirstNavigation("MainMenu")
                        'Select Case Session.kodeRole
                        '    Case 1
                        '        MainWindow.setFirstNavigation(Pages.Item("homeAdministrator"))
                        '    Case 2
                        '        MainWindow.setFirstNavigation(Pages.Item("homeFO"))
                        '    Case 3
                        '        MainWindow.setFirstNavigation(Pages.Item("kasirDokter"))
                        '    Case Else
                        '        MessageBox.Show("Peran tidak didefinisikan untuk user ini " & Session.kodeRole)
                        'End Select
                        'MainForm.roleid = dt.Rows(0).Item("koderole")
                    Else
                        MessageBox.Show("Username atau password salah. Silahkan coba lagi.")
                    End If
                Else
                    MessageBox.Show("Username atau password salah. Silahkan coba lagi.")
                End If
            Else
                MessageBox.Show("Password belum diisi.")
            End If
        Else
            MessageBox.Show("Username belum diisi.")
        End If
    End Sub
End Class
